<template>
  <div class="dark-login-wrap">
    <div class="dark-bg">
      <div class="stars"></div>
      <div class="moon"></div>
    </div>

    <div class="login-container">
      <div class="login-panel">
        <div class="panel-header">
          <img src="@/assets/logo.png" alt="Logo" class="logo" />
          <h2>ERP管理系统</h2>
          <p>欢迎登录</p>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="User"
              class="dark-input"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
              class="dark-input"
            />
          </el-form-item>

          <el-form-item>
            <div class="form-options">
              <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
              <el-link type="primary" :underline="false" @click="showForgotPassword">
                忘记密码？
              </el-link>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="dark-btn"
              :loading="loading"
              @click="handleLogin"
              style="width: 100%"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>

        <div class="panel-footer">
          <p>默认账号：<strong>admin</strong> / <strong>123456</strong></p>
        </div>
      </div>
    </div>

    <!-- 忘记密码对话框 -->
    <ForgotPasswordDialog
      v-model="forgotPasswordVisible"
      @success="handleResetSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import ForgotPasswordDialog from './components/ForgotPasswordDialog.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const emit = defineEmits(['login'])

const loginFormRef = ref()
const loading = ref(false)
const forgotPasswordVisible = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await userStore.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '登录成功')
      emit('login', response)
      
      const redirect = route.query.redirect
      if (redirect && redirect !== '/login') {
        router.push(decodeURIComponent(redirect))
      } else {
        router.push('/dashboard')
      }
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const showForgotPassword = () => {
  forgotPasswordVisible.value = true
}

const handleResetSuccess = () => {
  forgotPasswordVisible.value = false
}
</script>

<style scoped>
.dark-login-wrap {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a0e27;
  overflow: hidden;
}

.dark-bg {
  position: absolute;
  inset: 0;
}

.stars {
  position: absolute;
  inset: 0;
  background-image: 
    radial-gradient(2px 2px at 20% 30%, white, transparent),
    radial-gradient(2px 2px at 60% 70%, white, transparent),
    radial-gradient(1px 1px at 50% 50%, white, transparent),
    radial-gradient(1px 1px at 80% 10%, white, transparent),
    radial-gradient(2px 2px at 90% 60%, white, transparent),
    radial-gradient(1px 1px at 33% 80%, white, transparent),
    radial-gradient(1px 1px at 10% 40%, white, transparent);
  background-repeat: repeat;
  background-size: 200px 100px;
  animation: starsMove 20s linear infinite;
}

@keyframes starsMove {
  0% { transform: translateY(0); }
  100% { transform: translateY(-100px); }
}

.moon {
  position: absolute;
  top: 50px;
  right: 100px;
  width: 100px;
  height: 100px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 40px rgba(255, 255, 255, 0.3);
  animation: moonFloat 6s ease-in-out infinite;
}

@keyframes moonFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

.login-container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

.login-panel {
  background: rgba(26, 32, 54, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  padding: 40px;
}

.panel-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  height: 50px;
  filter: brightness(0) invert(1);
  margin-bottom: 16px;
}

.panel-header h2 {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
}

.panel-header p {
  margin: 0;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.login-form {
  margin-top: 20px;
}

.dark-input :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  box-shadow: none;
}

.dark-input :deep(.el-input__wrapper):hover {
  border-color: rgba(64, 158, 255, 0.5);
}

.dark-input :deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.dark-input :deep(.el-input__inner) {
  color: #fff;
}

.dark-input :deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.5);
}

:deep(.el-checkbox__label) {
  color: rgba(255, 255, 255, 0.8);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.dark-btn {
  border-radius: 8px;
  font-weight: 600;
  height: 48px;
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  border: none;
}

.dark-btn:hover {
  background: linear-gradient(135deg, #66b1ff 0%, #409eff 100%);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.4);
}

.panel-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.panel-footer p {
  margin: 0;
  color: rgba(255, 255, 255, 0.6);
  font-size: 12px;
}
</style>

